<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
			function Star(name, age) {
				this.name = name;
				this.age = age;
				// 构造函数
				// this.sing = function(){
				// 	console.log('唱了一首歌');
				// }
			}
			// 原型
			// Star.prototype.sing = function(){
			// 	console.log('我会唱歌');
			// }
			// Star.prototype.movie = function(){
			// 	console.log('我会演电影');
			// }
			Star.prototype = {
				//每个构造函数里面都会有一个原型对象constructor	
				constructor: Star,
				sing: function() {
					console.log('我会唱歌');
				},
				movie: function() {
					console.log('我会演电影');
				}
			}
			var star1 = new Star('刘德华', 20);
			var star2 = new Star('张学友', 18);
			// 构造函数每次都会开辟一个新的内存空间
			//console.log(star1.sing == star2.sing);//false
			//原型，指向一个内存空间
			console.log(star1.sing == star2.sing); //true
			console.log(Star.prototype.constructor);
			//console.log(star1.__proto__.constructor);
			console.log(Star.prototype);
			//console.log(star1.__proto__);
			console.log(Star.prototype.__proto__ === Object.prototype); //true
			
			//原型链查找机制:详细参考https://www.bilibili.com/video/BV1Wz4y1f7Mx?p=31
			//Star构造函数---->star1对象实例star1._proto_---->Star原型对象prototype._proto_---->object原型对象prototype._proto_-->null
			// 扩展内置对象
			Array.prototype.sum = function() {
				var sum = 0;
				for (var i = 0; i < this.length; i++) {
					sum += this[i];
				}
				return sum;
			}
			var arr1 = [1, 2, 3, 4];
			console.log(arr1.sum()); //10
			var arr2 = new Array(1, 2, 3);
			console.log(arr2.sum()); //6
			
			// 继承
			function fn(x, y) {
				console.log(this);
				console.log(x + y);
			}
			var o = {
				name: 'andy',
			}
			fn.call(); //Window
			fn.call(o, 1, 2); //{name: "andy"}
			
			//借用构造函数继承父类属性方法
			function Father(name, age) {
				this.name = name;
				this.age = age;
			}
			Father.prototype.money = function() {
				console.log(10000);
			}

			function Son(name, age, score) {
				Father.call(this, name, age);
				this.score = score;
			}
			// Son.prototype = Father.prototype子原型对象修改会导致复原型对象跟着修改
			// 使用对象的方式修改原型对象,利用constructor指回原来的原型对象
			Son.prototype = new Father();
			Son.prototype.constructor = Son;
			Son.prototype.exam = function() {
				console.log('考试1');
			}
			var son1 = new Son('Me', 18, 100);
			console.log(son1);
		</script>
	</body>
</html>
